﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;

namespace LprMiddleWare.Views
{
    /// <summary>
    /// Description for MainView.
    /// </summary>
    public partial class MainView : Window
    {

        private System.Windows.Forms.NotifyIcon _notifyIcon;
        private WindowState _oldWindowState;
        /// <summary>
        /// Initializes a new instance of the MainView class.
        /// </summary>
        public MainView()
        {
            InitializeComponent();

            _notifyIcon = new NotifyIcon();
            _notifyIcon.Icon = Properties.Resources.CarRepairBlue2;
            _notifyIcon.BalloonTipTitle = "车牌识别中间件";
            _notifyIcon.BalloonTipText = "正在后台运行，单击此处恢复窗口";
            _notifyIcon.Text = "车牌识别中间件";
            _notifyIcon.Click += new EventHandler(_notifyIcon_Click);
        }

        void _notifyIcon_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = _oldWindowState;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Hide();
                if (_notifyIcon != null)
                {
                    _notifyIcon.ShowBalloonTip(2000);
                }
            }
            else
            {
                _oldWindowState = this.WindowState;
            }
        }

        private void CheckTrayIcon()
        {
            ShowTrayIcon(!this.IsVisible);
        }

        private void ShowTrayIcon(bool show)
        {
            if (_notifyIcon != null)
            {
                _notifyIcon.Visible = show;
            }
        }

        private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            CheckTrayIcon();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (_notifyIcon != null)
            {
                _notifyIcon.Visible = false;
                _notifyIcon = null;
            }
        }
    }
}